//
//  ShowLargeImageController.m
//  QRMaster
//
//  Created by myApple on 16/4/14.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "ShowLargeImageController.h"
#import "UIImageView+WebCache.h"



@interface ShowLargeImageController ()
{
   __block UIImageView * imageView;
}

@end

@implementation ShowLargeImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"showLargeImage", @"查看图片");
//    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH /2.0 - 36, UI_SCREEN_HEIGHT /2.0- 36, 72, 72)];
//    imageView.image = _image;
//    [self.view addSubview:imageView];
    
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - 64)];
    if (_largeImageUrl == nil || [_largeImageUrl isEqualToString:@""]) {
        imageView.image = _image;
    }
    else
    {
        [imageView sd_setImageWithURL:[NSURL URLWithString:_largeImageUrl]];
    }
    [self.view addSubview:imageView];
    
    // Do any additional setup after loading the view.
}

-(void)viewDidAppear:(BOOL)animated
{
//    [self downLoadLargeImage];
}

-(void)downLoadLargeImage
{
    [self startWaiting:nil color:nil addToView:self.view];
    __weak ShowLargeImageController * weakSelf = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [weakSelf stopWaiting];
        UIImage * image = nil;
        if (weakSelf.largeImageUrl != nil && ![weakSelf.largeImageUrl isEqualToString:@""]) {
           image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_largeImageUrl]]];
        }
        else
        {
            image = weakSelf.image;
        }
        if (image == nil) {
            return ;
        }
      float width = image.size.width;
      float height = image.size.height;
    [UIView animateWithDuration:0.5 animations:^{
        CGRect rect = imageView.frame;
        if (width >= UI_SCREEN_WIDTH) {
            rect.origin.x = 0;
            rect.size.width = UI_SCREEN_WIDTH;
            rect.size.height = UI_SCREEN_WIDTH / width * height;
            rect.origin.y = (UI_SCREEN_HEIGHT - rect.size.height)/2.0;
            if (rect.size.height >= UI_SCREEN_HEIGHT - 64) {
                rect.size.height = UI_SCREEN_HEIGHT - 64;
                rect.origin.y = 0;
            }
            
        }
        else
        {
            rect.origin.x = (UI_SCREEN_WIDTH - width) / 2.0;
            rect.size.width = width;
            rect.size.height = UI_SCREEN_WIDTH / width * height;
            rect.origin.y = (UI_SCREEN_HEIGHT - rect.size.height)/2.0;
            if (rect.size.height >= UI_SCREEN_HEIGHT - 64) {
                rect.size.height = UI_SCREEN_HEIGHT - 64;
                rect.origin.y = 0;
                rect.size.width = (UI_SCREEN_HEIGHT - 64)/height * width;
                if (rect.size.width >= UI_SCREEN_WIDTH) {
                    rect.size.width = UI_SCREEN_WIDTH;
                }
                rect.origin.x = (UI_SCREEN_WIDTH - rect.size.width)/2.0;
            }
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
            imageView.frame = rect;
            imageView.image = image;
        });
        
    } completion:^(BOOL finished) {
        
    }];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
